import Vue from 'vue'
import VueRouter from 'vue-router'
import $Store from '@/store'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}



import Login from '@/pages/login'
import Register from '@/pages/login/register.vue'

import MixHome from '@/pages/MixHome.vue'

import UserProfile from '@/pages/user-profile'

import Index from '@/pages/index'

import User from '@/pages/system/user'
import Farmer from '@/pages/system/farmer'
import Category from '@/pages/system/category'
import Goods from '@/pages/system/goods'
import Order from '@/pages/system/order'
import ConsultInfo from '@/pages/system/consult/info.vue'
import ConsultReply from '@/pages/system/consult/reply.vue'

import Server from '@/pages/monitor/server'
import Druid from '@/pages/monitor/druid'
import Chain from '@/pages/monitor/chain'
import Online from '@/pages/monitor/online'
import Cache from '@/pages/monitor/cache'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: MixHome,
    children: [

      {
        path: 'user/profile',
        name: 'userProfile',
        component: UserProfile
      },

      {
        path: 'index',
        name: 'index',
        component: Index
      },

      {
        path: 'system/user',
        name: 'systemUser',
        component: User
      },
      {
        path: 'system/farmer',
        name: 'systemFarmer',
        component: Farmer
      },
      {
        path: 'system/category',
        name: 'systemCategory',
        component: Category
      },
      {
        path: 'system/goods',
        name: 'systemGoods',
        component: Goods
      },
      {
        path: 'system/order',
        name: 'systemOrder',
        component: Order
      },
      {
        path: 'system/consult/info',
        name: 'consultInfo',
        component: ConsultInfo
      },
      {
        path: 'system/consult/reply',
        name: 'consultReply',
        component: ConsultReply
      },

      {
        path: '/monitor/server',
        name: 'monitorServer',
        component: Server
      },
      {
        path: '/monitor/druid',
        name: 'monitorDruid',
        component: Druid
      },
      {
        path: '/monitor/chain',
        name: 'monitorChain',
        component: Chain
      },
      {
        path: '/monitor/online',
        name: 'monitorOnline',
        component: Online
      },
      {
        path: '/monitor/cache',
        name: 'monitorCache',
        component: Cache
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, form, next) => {
  if (to.path == '/login' || to.path == '/register') {
    next()
    return
  }

  let token = $Store.state.token
  if (!token) {
    next({
      path: '/login',
      redirect: to.fullPath
    })
    return
  }
  next()
})

export default router
